/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.imagelinkfixer;

import cz.mzk.kramerius.commons.DatastreamId;
import cz.mzk.kramerius.fedoraaccess.FedoraConnector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Pro monografii obsahující pdf v managed datastreamu IMG_FULL tento datastream odstraní a nahradí
 * novým (taky managed) ze souboru v pdfFile. Tento soubor musí být vidět z Fedory
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class PdfFixer extends AbstractFixer {

    private static final Logger logger = Logger.getLogger(PdfFixer.class.getName());
    private final String topLevelPid;
    private final String pdfFile;

    public PdfFixer(String topLevelPid, String pdfFile, FedoraConnector conn) {
        super(conn);
        this.topLevelPid = topLevelPid;
        this.pdfFile = pdfFile;
    }

    public void run() {
        DatastreamId dsId = DatastreamId.IMG_FULL;
        logger.log(Level.INFO, "fixing document {0}", topLevelPid);
        //drop datastream
        logger.log(Level.INFO, "removing datastream {0}", dsId.toString());
        conn.purgeDatastream(topLevelPid, dsId, "odstranění " + dsId.toString() + " kvůli opravě pdf");
        //create new datastream with content from file
        logger.log(Level.INFO, "adding managed datastream {0} from file {1}", new Object[]{dsId, pdfFile});
        String url = "file://".concat(pdfFile);
        conn.addManagedDatastreamByUrl(topLevelPid, dsId, url, "application/pdf", "Full monograph in pdf");
    }
}
